package cn.edu.jiangnan.cashmanagement.web.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.jiangnan.cashmanagement.common.bean.BaseBean;
import cn.edu.jiangnan.cashmanagement.entity.UserEntity;
import cn.edu.jiangnan.cashmanagement.service.user.IUserService;
import cn.edu.jiangnan.cashmanagement.util.FacesUtils;

@Component
@Scope(value="session")
public class UserBean extends BaseBean{

	private static final long serialVersionUID = 1L;

	@Autowired
	IUserService userService;
	private List<UserEntity>  userList;
 	private UserEntity  userInfo;
 	private String 		userName;
	private String		trueName;
	private String 		departmentName;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getTrueName() {
		return trueName;
	}
	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	
//	@PostConstruct
//	public void init
	
	public String  findUserInfoByConditions(){
		Map mp = new HashMap();
		mp.put("userName",userName);
		mp.put("trueName",trueName);
		mp.put("departmentName",departmentName);
		userList = userService.findUserByCondition(mp);
		return null;
	}
	
	//action listener event
	public void attrListener(ActionEvent event){
		setOperType((String)event.getComponent().getAttributes().get("operType"));
	}
	
	/**
	 * 更新员工信息
	 * */
	public String editUserInfo(){
		//获取参数值
		if(getOperType().equals(EDIT)){
			userInfo = userService.getUser(Long.valueOf(FacesUtils.getReqParamValue("userId")));
		}else if(getOperType().equals(NEW)){
			userInfo = null;
		}else if(getOperType().equals(VIEW)){
			userInfo = userService.getUser(Long.valueOf(FacesUtils.getReqParamValue("userId")));
		}
		return getOperType();
	}
	
	/**
	 * 删除某个用户的信息
	 * */
	public String deleteUser(){
		String userId = FacesUtils.getReqParamValue("userId");
		userService.deleteUserById(userId);
		return findUserInfoByConditions();
	}
	
	public List<UserEntity> getUserList(){
		return userList;
	}
	public UserEntity getUserInfo()
	{
		return userInfo;
	}
	public void setUserInfo(UserEntity userInfo)
	{
		this.userInfo = userInfo;
	}
}
